<template>
  <!-- 主题配置 -->
  <KoiDrawer ref="koiDrawerRef" title="主题配置" size="320" :footerHidden="true" :closeOnClickModel="true">
    <template #content>
      <div class="p-t-8px select-none">
        <!-- 主题颜色选择器 -->
        <div class="config-section">
          <div class="section-header">
            <el-icon :size="18" class="section-icon"><Connection /></el-icon>
            <span class="section-title">主题颜色</span>
          </div>

          <div class="theme-colors-grid">
            <div
              v-for="color in themeColors"
              :key="color"
              class="theme-color-item"
              @click="changeThemeColor(color)"
              :class="{ active: globalStore.themeColor === color }"
            >
              <div class="color-preview" :style="{ backgroundColor: color }">
                <div class="color-check" v-if="globalStore.themeColor === color">
                  <el-icon><Check /></el-icon>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 界面配置 -->
      <div class="config-section">
        <div class="section-header">
          <el-icon :size="18" class="section-icon"><ChatLineRound /></el-icon>
          <span class="section-title">界面配置</span>
        </div>
        <div class="interface-config">
          <div class="config-item">
            <div class="config-label">灰色模式</div>
            <el-switch
              active-text="启用"
              inactive-text="停用"
              :active-value="true"
              :inactive-value="false"
              :inline-prompt="true"
              v-model="isGrey"
              @change="changeGreyOrWeak('grey', !!$event)"
            />
          </div>

          <div class="config-item">
            <div class="config-label">色弱模式</div>
            <el-switch
              active-text="启用"
              inactive-text="停用"
              :active-value="true"
              :inactive-value="false"
              :inline-prompt="true"
              v-model="isWeak"
              @change="changeGreyOrWeak('weak', !!$event)"
            />
          </div>

          <div class="config-item">
            <div class="config-label">MarkDown</div>
            <el-dropdown @command="handlePreviewCommand">
              <el-button type="primary" size="small" plain class="!outline-none">
                预览主题<el-icon class="el-icon--right"><arrow-down /></el-icon>
              </el-button>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="default" :disabled="globalStore.previewTheme == 'default'">default</el-dropdown-item>
                  <el-dropdown-item command="github" :disabled="globalStore.previewTheme == 'github'">github</el-dropdown-item>
                  <el-dropdown-item command="vuepress" :disabled="globalStore.previewTheme == 'vuepress'"
                    >vuepress</el-dropdown-item
                  >
                  <el-dropdown-item command="mk-cute" :disabled="globalStore.previewTheme == 'mk-cute'">mk-cute</el-dropdown-item>
                  <el-dropdown-item command="smart-blue" :disabled="globalStore.previewTheme == 'smart-blue'"
                    >smart-blue</el-dropdown-item
                  >
                  <el-dropdown-item command="cyanosis" :disabled="globalStore.previewTheme == 'cyanosis'"
                    >cyanosis</el-dropdown-item
                  >
                  <el-dropdown-item command="codeTheme" :disabled="globalStore.previewTheme == 'codeTheme'"
                    >codeTheme</el-dropdown-item
                  >
                </el-dropdown-menu>
              </template>
            </el-dropdown>
            <el-dropdown class="m-l-6px" @command="handleCodeCommand">
              <el-button type="primary" size="small" plain class="!outline-none">
                代码主题<el-icon class="el-icon--right"><arrow-down /></el-icon>
              </el-button>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="atom" :disabled="globalStore.codeTheme == 'atom'">atom</el-dropdown-item>
                  <el-dropdown-item command="a11y" :disabled="globalStore.codeTheme == 'a11y'">a11y</el-dropdown-item>
                  <el-dropdown-item command="github" :disabled="globalStore.codeTheme == 'github'">github</el-dropdown-item>
                  <el-dropdown-item command="gradient" :disabled="globalStore.codeTheme == 'gradient'">gradient</el-dropdown-item>
                  <el-dropdown-item command="kimbie" :disabled="globalStore.codeTheme == 'kimbie'">kimbie</el-dropdown-item>
                  <el-dropdown-item command="paraiso" :disabled="globalStore.codeTheme == 'paraiso'">paraiso</el-dropdown-item>
                  <el-dropdown-item command="qtcreator" :disabled="globalStore.codeTheme == 'qtcreator'"
                    >qtcreator</el-dropdown-item
                  >
                  <el-dropdown-item command="stackoverflow" :disabled="globalStore.codeTheme == 'stackoverflow'"
                    >stackoverflow</el-dropdown-item
                  >
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </div>
    </template>
  </KoiDrawer>
</template>

<script setup lang="ts">
import { ref } from "vue";
// 明亮主题和暗黑主题配色工具类
import { useTheme } from "@/utils/theme.ts";
import { storeToRefs } from "pinia";
import mittBus from "@/utils/mittBus.ts";
import useGlobalStore from "@/stores/modules/global.ts";

const globalStore = useGlobalStore();

const { changeThemeColor, changeGreyOrWeak } = useTheme();
const { isGrey, isWeak } = storeToRefs(globalStore);

// 主题颜色配置
const themeColors = [
  "#2992FF",
  "#1E71EE",
  "#6169FF",
  "#8076C3",
  "#1BA784",
  "#316C72",
  "#FF6B35",
  "#0099FF",
  "#EF4444",
  "#BE967F",
  "#EC4899",
  "#06B6D4"
];

// 主题设置对话框Ref
const koiDrawerRef = ref();

/** 打开主题配置 */
const handleThemeConfig = () => {
  koiDrawerRef.value.koiOpen();
};

/** 打开主题配置对话框，on 接收事件 */
mittBus.on("handleThemeConfig", () => {
  handleThemeConfig();
});

/** 预览主题 */
const handlePreviewCommand = (command: string) => {
  globalStore.setGlobalState("previewTheme", command);
};

/** 代码主题 */
const handleCodeCommand = (command: string) => {
  globalStore.setGlobalState("codeTheme", command);
};
</script>

<style lang="scss" scoped>
.config-section {
  margin-bottom: 20px;
}

.section-header {
  display: flex;
  align-items: center;
  padding-bottom: 16px;
  margin-bottom: 20px;

  .section-icon {
    margin-right: 12px;
    font-size: 18px;
    color: var(--el-color-primary);
    opacity: 0.9;
    transition: all 0.3s ease;
  }

  .section-title {
    position: relative;
    font-size: 16px;
    font-weight: 600;
    color: var(--el-text-color-primary);
    letter-spacing: 0.3px;

    &::after {
      position: absolute;
      bottom: -8px;
      left: 0;
      width: 40px;
      height: 3px;
      content: "";
      background: linear-gradient(135deg, var(--el-color-primary) 0%, var(--el-color-primary-light-3) 50%, transparent 100%);
      border-radius: 2px;
      box-shadow: 0 2px 8px rgba(var(--el-color-primary-rgb), 0.3);
      animation: title-underline 3s ease-in-out infinite;
    }

    &::before {
      position: absolute;
      top: -2px;
      left: -4px;
      width: 6px;
      height: 6px;
      content: "";
      background: var(--el-color-primary);
      border-radius: 50%;
      opacity: 0.6;
      animation: title-dot 2s ease-in-out infinite;
    }
  }

  &:hover {
    .section-icon {
      opacity: 1;
      transform: scale(1.1);
    }

    .section-title::after {
      width: 50px;
      animation: title-underline-hover 0.6s ease forwards;
    }
  }
}

/** 主题颜色选择器 */
.theme-colors-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;

  .theme-color-item {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 8px 6px;
    cursor: pointer;
    border: 2px solid transparent;
    border-radius: 10px;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    background: var(--el-bg-color);

    &:hover {
      background-color: var(--el-fill-color-light);
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
      transform: translateY(-2px);

      .color-preview {
        transform: scale(1.05);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
        animation: color-preview-hover 0.6s ease-in-out infinite;

        &::before {
          opacity: 1;
          animation: shimmer-sweep 1.5s ease-in-out infinite;
        }
      }
    }

    &.active {
      background-color: var(--el-color-primary-light-9);
      border-color: var(--el-color-primary);
      box-shadow: 0 2px 8px rgba(var(--el-color-primary-rgb), 0.15);

      .color-preview {
        transform: scale(1.02);
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
        animation: color-pulse 2s ease-in-out infinite;
      }
    }

    .color-preview {
      width: 32px;
      height: 32px;
      border-radius: 6px;
      box-shadow: 0 3px 8px rgba(0, 0, 0, 0.12);
      transition: all 0.3s ease;
      position: relative;
      overflow: hidden;
      display: flex;
      align-items: center;
      justify-content: center;

      &::before {
        content: "";
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: linear-gradient(45deg, transparent 30%, rgba(255, 255, 255, 0.1) 50%, transparent 70%);
        opacity: 0;
        transform: translateX(-100%);
        transition: opacity 0.3s ease;
      }

      &:hover {
        transform: scale(1.1);
        box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
      }
    }

    .color-check {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 16px;
      height: 16px;
      background: rgba(255, 255, 255, 0.95);
      border-radius: 50%;
      color: var(--el-color-primary);
      font-size: 10px;
      font-weight: bold;
      box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
      backdrop-filter: blur(4px);
      animation: fade-in-scale 0.3s ease;

      &::before {
        content: "";
        position: absolute;
        top: -2px;
        left: -2px;
        right: -2px;
        bottom: -2px;
        background: linear-gradient(45deg, rgba(255, 255, 255, 0.8), rgba(255, 255, 255, 0.4));
        border-radius: 50%;
        z-index: -1;
      }
    }
  }
}

/** 界面配置 */
.interface-config {
  .config-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 0;
    border-bottom: 1px solid var(--el-border-color-extra-light);

    &:last-child {
      border-bottom: none;
    }

    .config-label {
      display: flex;
      align-items: center;
      flex: 1;
      min-width: 0;
      margin-right: 16px;
      font-size: 14px;
      color: var(--el-text-color-primary);
      font-weight: 500;
      line-height: 1.4;

      .warning-icon {
        margin-left: 2px;
        color: var(--el-text-color-secondary);
        cursor: help;
        transition: color 0.2s ease;
        flex-shrink: 0;

        &:hover {
          color: var(--el-color-primary);
        }
      }
    }

    .config-input {
      width: 180px;
      flex-shrink: 1;
    }
  }
}

/** 动画 */
@keyframes fade-in-scale {
  0% {
    opacity: 0;
    transform: scale(0.6);
  }
  50% {
    transform: scale(1.1);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes title-underline {
  0%,
  100% {
    transform: scaleX(1);
    opacity: 0.8;
  }
  50% {
    transform: scaleX(1.1);
    opacity: 1;
  }
}

@keyframes title-underline-hover {
  0% {
    transform: scaleX(1);
  }
  50% {
    transform: scaleX(1.2);
  }
  100% {
    transform: scaleX(1);
  }
}

@keyframes title-dot {
  0%,
  100% {
    transform: scale(1);
    opacity: 0.6;
  }
  50% {
    transform: scale(1.3);
    opacity: 0.9;
  }
}

@keyframes color-pulse {
  0%,
  100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.02);
  }
}

@keyframes color-preview-hover {
  0%,
  100% {
    transform: scale(1.05);
  }
  50% {
    transform: scale(1.08);
  }
}

@keyframes shimmer-sweep {
  0% {
    transform: translateX(-100%);
  }
  50% {
    transform: translateX(100%);
  }
  100% {
    transform: translateX(100%);
  }
}
</style>
